package com.jic.point.controller.feign;

import com.alibaba.fastjson.JSON;
import com.jic.common.base.vo.RestResult;
import com.jic.point.api.PointCentreApi;
import com.jic.point.model.entity.*;
import com.jic.point.rest.request.PointRecordGrantStatus;
import com.jic.point.rest.request.SignData;
import com.jic.point.service.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName PointCentreClient
 * TODO
 * @Description
 * @Author chenhongfeng
 * @Date 2020/11/25 16:00
 * @Version 1.0
 **/
@Api(tags = {"Feign-积分中心"})
@RequestMapping(value = "/api/point/centre")
@RestController
@Slf4j
public class PointCentreClient implements PointCentreApi {
    @Resource
    private HaierSmartHomePointService haierSmartHomePointService;

    @Resource//第三方机构
    private ConfThirdPartLogService confThirdPartService;
    @Resource//平台
    private ConfPlatformLogService confPlatformService;
    @Resource//部门
    private ConfBdPointLogService confBdService;
    @Resource//供应商
    private ConfSupplierLogService confSupplierService;
    @Resource//活动规则
    private ConfActivityRuleLogService confActivityRuleService;

    @Override
    @PostMapping("getPointRecordGrantStatus")
    @ApiOperation(value = "  按流水号、来源查询积分发放状态")
    public RestResult<?> getPointRecordGrantStatus(PointRecordGrantStatus request) {
        log.info("入参{}", JSON.toJSONString(request));
       RestResult restResult = checkPointsReceive(request);
        if(restResult!=null) return restResult;
        RestResult restResult1 = checkPointsReceive2(request);
        if(restResult1!=null)return restResult1;
        log.info("出参{}", JSON.toJSONString(restResult1));
        return null;
    }



    //积分领取  校验主体  第一层校验
    public RestResult checkPointsReceive(SignData signData) {
        log.info("入参{}", JSON.toJSONString(signData));
        //查询参数业务类型和编码是否为空
        if (signData.getBusiType() != null && "".equals(signData.getBusiType()) && signData.getBusiCode() != null) return RestResult.error("0001", "业务类型或业务类型对应编码不能为空");
        //通过业务类型查询数据
        List<SignData> signData1 = haierSmartHomePointService.selAuthorityList(signData);
        if (signData1.size() < 1) return RestResult.error("0002", "当前操作，业务类型以及编码不存在");
        SignData sign = signData1.get(0);
        if (sign.getStatus() != 2) return RestResult.error("0003", "当前操作，渠道未启用！请先启用");
        if (sign.getAppKey() != null && sign.getSecreKey() != null) return RestResult.error("0004", "标识码和秘钥为空或不匹配");
        //判断是否有查询操作权限
        if (sign.getQueryType() != 1) return RestResult.error("0005", "当前查询操作，无权限进行查询积分的操作！");
        return null;
    }

    //积分领取或消耗 第二层校验
    public RestResult checkPointsReceive2(SignData signData) {
        log.info("入参{}", JSON.toJSONString(signData));
        //如果新增 需要有相应的权限
        //如果是平台 必须有唯一绑定结算关系
        //调用接口的是BD，给某个部门增加或消耗积分时，必须要【1、预算关系】，不然不能新增/消耗积分；
        //  `sts` int(2) DEFAULT '0' COMMENT '状态（-3、双向冻结；-2、流入冻结，流出正常，-1、停用，0、初始化；1、启用；2、流入正常，流出冻结；）',
        //第三方机构校验
        if ("THRID_PART_PLATFORM".equals(signData.getBusiType())) {
            log.info("入参{}", JSON.toJSONString(signData));
            List<ConfThirdPartLog> confThirdParts = confThirdPartService.selThirdPartByCode(signData);
            if (confThirdParts.size() < 1) return RestResult.error("0011", "第三方机构编码有误或不存在！");
            return RestResult.success(confThirdParts);
        }
        //平台校验
        if ("PLATFORM".equals(signData.getBusiType())) {
            log.info("入参{}", JSON.toJSONString(signData));
            List<ConfPlatformLog> confPlatforms = confPlatformService.selectByPrimaryCode(signData);
            if (confPlatforms.size() < 1) return RestResult.error("0011", "平台编码有误或不存在！");
            return RestResult.success(confPlatforms);
        }
        //部门校验
        if ("DEPARTMENT".equals(signData.getBusiType())) {
            log.info("入参{}", JSON.toJSONString(signData));
            List<ConfBdPointLog> confBds = confBdService.selConfBdByCode(signData);
            if (confBds.size() < 1) return RestResult.error("0011", "部门编码有误或不存在！");
            return RestResult.success(confBds);

        }
        //供应商
        if ("SUPPLY".equals(signData.getBusiType())) {
            log.info("入参{}", JSON.toJSONString(signData));
            List<ConfSupplierLog> confSuppliers = confSupplierService.selConfSupplierByCode(signData);
            if (confSuppliers.size() < 1) return RestResult.error("0011", "供应商编码有误或不存在！");
            return RestResult.success(confSuppliers);
        }
        //活动规则
        if ("ACTIVITY_RULE".equals(signData.getBusiType())) {
            log.info("入参{}", JSON.toJSONString(signData));
            List<ConfActivityRuleLog> confActivityRules = confActivityRuleService.selConfActivityRuleByCode(signData);
            if (confActivityRules.size() < 1) return RestResult.error("0011", "活动规则编码有误或活动规则不存在！");
            return RestResult.success(confActivityRules);
        }
        return null;
    }
}
